#include "pch.h"
#include "LinkQueue.h"

void InitQueue(LinkQueue *q) {
	QueueNode *p = (QueueNode*)malloc(sizeof(QueueNode));
	assert(p != NULL);
	p->next = NULL;
	q->front = q->tail = p;
}

void EnQueue(LinkQueue *q, ElemType x) {
	QueueNode *s = (QueueNode*)malloc(sizeof(QueueNode));
	assert(s != NULL);
	s->data = x;
	s->next = NULL;
	q->tail->next = s;
	q->tail = s;
}

void ShowQueue(LinkQueue *q) {
	QueueNode *p = q->front->next;
	printf("front:>");
	while (p != NULL) {
		printf("%d", p->data);
		p = p->next;
	}
	printf(">:tail.\n");
}

void DeQueue(LinkQueue *q) {
	if (q->front == q->tail)
		return;
	QueueNode *p = q->front->next;
	q->front->next = p->next;
	free(p);
	if (p == q->tail)
		q->tail = q->front;
}

void GetHead(LinkQueue *q, ElemType *v) {
	if (q->front == q->tail)
		return;
	QueueNode *p = q->front->next;
	*v = p->data;
}

int Length(LinkQueue *q) {
	int len = 0;
	QueueNode *p = q->front->next;
	while (p != NULL) {
		len++;
		p = p->next;
	}
	return len;
}

void ClearQueue(LinkQueue *q) {
	if (q->front == q->tail)
		return;
	QueueNode *p = q->front->next;
	while (p != NULL) {
		q->front->next = p->next;
		free(p);
		p = q->front->next;
	}
	q->tail = q->front;
}

void DestroyQueue(LinkQueue *q) {
	ClearQueue(q);
	free(q->front);
	q->front = q->tail = NULL;
}